package hlhj.fhp.supreme.activitys

import android.content.Intent
import android.support.v7.app.AlertDialog
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import android.widget.CheckBox
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.BaseAty
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.javabean.BaseBean
import hlhj.fhp.supreme.javabean.CollocationBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.utils.JsonCallBack
import hlhj.fhp.supreme.utils.MyUtils
import kotlinx.android.synthetic.main.activity_my_collections_aty.*
import java.util.ArrayList

class MyCollectionsAty : BaseAty() {
    private val datas=ArrayList<CollocationBean.DataBean>()
    private var isEdit=false
    private lateinit var adapter:BaseQuickAdapter<CollocationBean.DataBean,BaseViewHolder>
    private val checkedHash=HashMap<Int,Boolean>()
    override fun getContentId(): Int {
        return R.layout.activity_my_collections_aty
    }
    override fun initView() {
        adapter=object :BaseQuickAdapter<CollocationBean.DataBean,BaseViewHolder>(R.layout.mycollection_item,datas){
            override fun convert(helper: BaseViewHolder?, item: CollocationBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.title)
                helper?.setText(R.id.itemTime,MyUtils.timeStampToStrYMD(item?.create_time!!.toLong()))
                helper?.setText(R.id.itemKind,"分类：${item?.cat_name}")
                Glide.with(mContext).load(item?.img).into(helper?.getView(R.id.itemIcon))
                if (isEdit){
                    helper?.setVisible(R.id.btIsChecked,true)
                }else{
                    helper?.setVisible(R.id.btIsChecked,false)
                }
                val checkBox = helper?.getView<CheckBox>(R.id.btIsChecked)
                checkBox?.isChecked = checkedHash[helper?.adapterPosition]!!
                checkBox?.setOnCheckedChangeListener { buttonView, isChecked ->
                    checkedHash.put(helper?.adapterPosition,isChecked)
                }
                helper?.getView<TextView>(R.id.btDelet)?.setOnClickListener {
                    AlertDialog.Builder(mContext).setTitle("取消收藏？")
                            .setPositiveButton("确定"){
                                dialog, which ->
                                OkGo.post<BaseBean>(Urls.BaseUrl+Urls.FOLLOW)
                                        .params("title",item?.title)
                                        .params("cat_name",item?.cat_name)
                                        .params("id",item?.object_id!!)
                                        .params("type",item?.type)
                                        .params("img",item?.img)
                                        .execute(object :JsonCallBack<BaseBean>(this@MyCollectionsAty){
                                            override fun onSuccess(response: Response<BaseBean>?) {
                                                val bean = response?.body()
                                                if (bean?.code==1){
                                                    MyUtils.toast("取消成功")
                                                    disPatchData()
                                                }else{
                                                    MyUtils.toast(bean?.msg)
                                                }
                                                dialog.dismiss()
                                            }
                                        })
                            }
                            .setNegativeButton("取消"){
                                dialog, which ->
                                dialog.dismiss()
                            }
                            .show()
                }
                val layout = helper?.getView<RelativeLayout>(R.id.lo)
                layout?.setOnLongClickListener (object :View.OnLongClickListener{
                    override fun onLongClick(v: View?): Boolean {
                        AlertDialog.Builder(mContext).setTitle("取消收藏？")
                                .setPositiveButton("确定"){
                                    dialog, which ->
                                    OkGo.post<BaseBean>(Urls.BaseUrl+Urls.FOLLOW)
                                            .params("title",item?.title)
                                            .params("cat_name",item?.cat_name)
                                            .params("id",item?.object_id!!)
                                            .params("type",item?.type)
                                            .params("img",item?.img)
                                            .execute(object :JsonCallBack<BaseBean>(this@MyCollectionsAty){
                                                override fun onSuccess(response: Response<BaseBean>?) {
                                                    val bean = response?.body()
                                                    if (bean?.code==1){
                                                        MyUtils.toast("取消成功")
                                                        disPatchData()
                                                    }else{
                                                        MyUtils.toast(bean?.msg)
                                                    }
                                                    dialog.dismiss()
                                                }
                                            })
                                }
                                .setNegativeButton("取消"){
                                    dialog, which ->
                                    dialog.dismiss()
                                }
                                .show()
                        return false
                    }
                })
            }

        }
        listView.adapter=adapter
        listView.layoutManager=LinearLayoutManager(this,LinearLayout.VERTICAL,false)
        disPatchData()
    }
    private fun disPatchData() {
        OkGo.post<CollocationBean>(Urls.BaseUrl + Urls.COLLOCTION)
                .execute(object : JsonCallBack<CollocationBean>(this) {
                    override fun onSuccess(response: Response<CollocationBean>?) {
                        val bean = response?.body()
                        if (bean?.code == 1) {
                            datas.clear()
                            datas.addAll(bean?.data)
                            for (i in 0..bean?.data.size-1){
                                checkedHash.put(i,false)
                            }
                            adapter.notifyDataSetChanged()
                        }
                    }
                })
    }
    override fun initListener() {
        btEdit.setOnClickListener {
            isEdit=!isEdit
            if (isEdit){
                btDelet.visibility=View.VISIBLE
            }else{
                btDelet.visibility=View.GONE
            }
            adapter.notifyDataSetChanged()
        }
        btExit.setOnClickListener{
            finish()
        }
        adapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(this, OptionDetailAty::class.java)
            intent.putExtra("id",datas[position].object_id)
            intent.putExtra("type",datas[position].type)
            intent.putExtra("cat_name",datas[position].cat_name )
            startActivity(intent)
        }
        btDelet.setOnClickListener {
            var ids=""
            for(i in 0..checkedHash.size-1){
                if (checkedHash[i]!!){
                    if (i==0){
                        ids=datas[i].id.toString()+","
                    }else{
                        ids=ids+",${datas[i].id.toString()}"
                    }
                }
            }
            OkGo.post<BaseBean>(Urls.BaseUrl+Urls.GroupDelet)
                    .params("ids",ids)
                    .execute(object :JsonCallBack<BaseBean>(this){
                        override fun onSuccess(response: Response<BaseBean>?) {
                            val baseBean = response?.body()
                            if (baseBean!!.code==1){
                                MyUtils.toast("删除成功")
                                disPatchData()
                            }else{
                                MyUtils.toast(baseBean?.msg)
                            }
                        }
                    })
        }

    }
}
